home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / rng / knuthran2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-02  |  2.2 KB  |  95 lines

  1. /* rng/knuthran2.c
  2.  * 
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or (at
  6.  * your option) any later version.
  7.  * 
  8.  * This program is distributed in the hope that it will be useful, but
  9.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.  * General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License
  14.  * along with this program; if not, write to the Free Software
  15.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /*
  19.  * This generator is taken from
  20.  *
  21.  * Donald E. Knuth
  22.  * The Art of Computer Programming
  23.  * Volume 2
  24.  * Third Edition
  25.  * Addison-Wesley
  26.  * Page 108
  27.  *
  28.  * This implementation copyright (C) 2001 Carlo Perassi.
  29.  */
  30.  
  31. #include <config.h>
  32. #include <stdlib.h>
  33. #include <gsl/gsl_rng.h>
  34.  
  35. #define AA1 271828183UL
  36. #define AA2 314159269UL
  37. #define MM 0x7fffffffUL        /* 2 ^ 31 - 1 */
  38.  
  39. static inline unsigned long int ran_get (void *vstate);
  40. static double ran_get_double (void *vstate);
  41. static void ran_set (void *state, unsigned long int s);
  42.  
  43. typedef struct
  44. {
  45.   unsigned long int x0;
  46.   unsigned long int x1;
  47. }
  48. ran_state_t;
  49.  
  50. static inline unsigned long int
  51. ran_get (void *vstate)
  52. {
  53.   ran_state_t *state = (ran_state_t *) vstate;
  54.  
  55.   unsigned long int xtmp = state->x1;
  56.   state->x1 = (AA1 * state->x1 - AA2 * state->x0) & MM;
  57.   state->x0 = xtmp;
  58.  
  59.   return state->x1;
  60. }
  61.  
  62. static double
  63. ran_get_double (void *vstate)
  64. {
  65.   ran_state_t *state = (ran_state_t *) vstate;
  66.  
  67.   return ran_get (state) / 2147483648.0;
  68. }
  69.  
  70. static void
  71. ran_set (void *vstate, unsigned long int s)
  72. {
  73.   ran_state_t *state = (ran_state_t *) vstate;
  74.  
  75.   if (s == 0)
  76.     s = 1;            /* default seed is 1 */
  77.  
  78.   state->x0 = s & MM;
  79.   state->x1 = s & MM;
  80.  
  81.   return;
  82. }
  83.  
  84. static const gsl_rng_type ran_type = {
  85.   "knuthran2",            /* name */
  86.   MM,                /* RAND_MAX */
  87.   0,                /* RAND_MIN */
  88.   sizeof (ran_state_t),
  89.   &ran_set,
  90.   &ran_get,
  91.   &ran_get_double
  92. };
  93.  
  94. const gsl_rng_type *gsl_rng_knuthran2 = &ran_type;
  95.